<?php
/**
 * Created by PhpStorm.
 * User: lelouchms
 * Date: 2019/6/3
 * Time: 10:46
 */

namespace app\index\controller;


use app\common\exception\CustomerException;
use app\common\model\UserAppointment;
use app\common\model\UserHouseType;

class UserHouse extends Base
{

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 租房方式
     * @return mixed
     */
    public function types(){
        return (new UserHouseType())->select();
    }

    /**
     * 我的租房信息
     * @return array|\PDOStatement|string|\think\Collection
     */
    public function index()
    {
        $page = $this->request->get("page", 1);
        $limit = $this->request->get("limit", 10);
        $userId = session("web_user")['id'];
        $userHouseModel = new \app\common\model\UserHouse();
        $houseList = $userHouseModel->getUserHouse($userId, $page, $limit);
        return $houseList;
    }

    /**
     * 用户当前的租房信息
     * @return \app\common\model\House|array
     */
    public function getCurrentUserHouse()
    {
        $userHouseModel = new \app\common\model\UserHouse();
        $houseList = $userHouseModel->getUserCurrentHouse(session("web_user")['id']);
        return $houseList;
    }

    /** 用户预约
     * @return string
     * @throws CustomerException
     */
    public function appointment()
    {
        $requestData = $this->request->only(["house_id", "username", "phone", "time", "remark"]);
        $requestData['user_id'] = session("web_user")["id"];
        $requestData['status'] = 1;
        $validate = new \app\common\validate\UserAppointment();
        if (!$validate->scene("add")->check($requestData)) {
            throw new CustomerException($validate->getError());
        }
        (new UserAppointment())->save($requestData);
        return "申请成功";
    }


}